const app = getApp();
Page({
  onLoad(options) {
    let { id } = options;
    this.setData({ id });
    this.getVideoList(id);
  },

  getVideoList(id) {
    app.util.request({
      url: 'entry/wxapp/index',
      data: {
        op: 'getReviewList',
        id,
      },
      module: 'cm_lvb',
      method: 'post',
      cachetime: 0,
      success: res => {
        let fileSet = [];
        res.data.errno === 0 
        && res.data.data.lists 
        && res.data.data.lists.fileSet 
        && (fileSet = res.data.data.lists.fileSet);
        fileSet.forEach(item => {
          item.duration = this.formatTime(item.duration);
        });
        this.setData({
          videoList: fileSet,
          defaultVideo: fileSet[0] || {},
        });
      }, 
    });
  },

  playhistory(e) {
    let { fileId } = e.currentTarget.dataset;
    fileId && this.setData({
      defaultVideo: this.data.videoList.find(item => item.fileId === fileId) || {},
    });
  },

  // 将秒转为时长
  formatTime(value) {
    let theTime = parseInt(value, 10);
    let theTime1 = 0; 
    let theTime2 = 0;
    if (theTime > 60) {
      theTime1 = parseInt(theTime / 60, 10);
      theTime = parseInt(theTime % 60, 10);
      if (theTime1 > 60) {
        theTime2 = parseInt(theTime1 / 60, 10);
        theTime1 = parseInt(theTime1 % 60, 10);
      }
    }
    let result = String(parseInt(theTime, 10) + '秒');
    if (theTime1 > 0) {
      result = String(parseInt(theTime1, 10) + '分' + result);
    }
    if (theTime2 > 0) {
      result = String(parseInt(theTime2, 10) + '小时' + result);
    }
    return result;  
  },
});